// 商城首页
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noMore:false,
    // 轮播图
    imgUrls:[],
    // 搜索条件
    search:"",
    // 分类
    classify:[],
    // 产品
    products:[],
    // 分页
    P:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var shop_id = app.globalData.shop_id;
    var p = 1;
    let that = this;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Index/index',
      data:{
        shop_id: shop_id,
          p:p
      },
      header:{
        "content-type":"application/json"
      },
      success:function(res){
        console.log(res)
        var headPic = res.data.data.head_pic;
        app.globalData.headPic = headPic;
        // 分类
        var category = res.data.data.category;
        // 轮播图
        var advert = res.data.data.advert;
        var product = res.data.data.goods_list;
        that.setData({
          products:product,
          classify: category,
          imgUrls: advert,
          p:p
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取购物车数量
   
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var shop_id = app.globalData.shop_id;
    var p = 1;
    let that = this;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Index/index',
      data: {
        shop_id: shop_id,
        p: p
      },
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        console.log(res)
        // 分类
        var category = res.data.data.category;
        // 轮播图
        var advert = res.data.data.advert;
        var product = res.data.data.goods_list;
        that.setData({
          products: product,
          classify: category,
          imgUrls: advert,
          p: p,
          noMore: false
        })
      }
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数，加载第二页的商品
   */
  onReachBottom: function () {
    var noMore = this.data.noMore;
    if(noMore == false){
    var p = this.data.p;
    p = p + 1;
    var shop_id = app.globalData.shop_id;
    let that = this;
    var products = this.data.products;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Index/index',
      data: {
        shop_id: shop_id,
        p: p
      },
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        console.log(res)
        var product = res.data.data.goods_list;
        if(product != ""){
          products.push(product);
        }else{
          noMore = true;
        }
        that.setData({
          products: products,
          p: p,
          noMore: noMore
        })
      }
    })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.name,
      path: "/pages/shouye/shouye",
      imageUrl: app.globalData.headPic
    }
  },
  /**
   * 分类信息跳转
   */
  intoClassify:function(e){
    var index = e.currentTarget.dataset.index;
    wx.setStorage({
      key: 'idx',
      data: index,
    })
    wx.navigateTo({
      url: '/pageA/classify/classify',
    })
  },
  /**
   * 搜索跳转
   */
  search:function(){
    wx.navigateTo({
      url: '/pageA/search/search',
    })
  },
  /**
   * 进入商品详情页
   */
  detail:function(e){
    var index = e.currentTarget.dataset.index;
    var products = this.data.products;
    var product = products[index]
    app.globalData.productModel = product;
    wx.navigateTo({
      url: '/pageA/detail/detail',
    })
  },
  // 轮播图点击跳转，打开外部连接（判断是否为小程序内部页面,为内部链接，需要在url中带参数pid=）
  lunbo:function(e){
    var index = e.currentTarget.dataset.index;
    var imgUrls = this.data.imgUrls;
    var url = imgUrls[index].url;
    var urls = url.split("/")
    var sort = urls[0]+"//"+urls[2] +"/"
    if(urls[0]==http || urls[0]==https){
      wx.setStorage({
        key: 'url',
        data: url,
        success: function () {
          wx.navigateTo({
            url: "/pages/outsite/outsite"
          })
        }
      }) 
    }else{
      wx.navigateTo({
        url: url,
      })
    }
   
  }
})